var app = getApp()
import ajaxhelper from '../../utils/ajaxhelper.js';
import { signByMd5 } from "../../utils/util";
import checkLogin from '../../utils/checkLogin.js';
var gloalAudio = require("../common/gloalAudio/gloalAudio.js");//全局音乐模块
Page({
  data: {
    isPlayGloalAudio: false,
    amount:0.00,
    dataList:[],
    hasRefresh: false,
    isGetMore: false,
    hasMore: true,
    pageNo: 1,
    isShow: false,//是否显示没有列表的提示图

  },
  onLoad(options) {
    console.log(options);
    // app.checkPlay(this);
    // 检查有没有登录，第二个参数是回调函数
    checkLogin.login(this, function (res) {
      console.log(res)
    })
    //查询余额
    this.getBalance();
    //查询列表
    this.getList()
  },
  onShow() {
    app.checkPlay(this);//检测全局有没有音频播放
  },
  //下拉刷新
  onPullDownRefresh() {
    this.getModuleData();
    // 检查有没有登录，第二个参数是回调函数
    checkLogin.login(this, function (res) {
      console.log(res)
    })
    app.checkPlay(this);//检测全局有没有音频播放
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1400);
  },
  
  // 全局音乐弹框
  showGloalAudio() {
    gloalAudio.set('', {
      confirm: function (data) {
        console.log(data)
      }.bind(this)
    });
    gloalAudio.open('')
  },
 //余额
 getBalance:function(){
   var param = {}
   var token = (wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token) || '';
   ajaxhelper.get(app.globalData.frontJSHost + 'hongbao/account?token=' + encodeURIComponent(token), param, this, function (res) {
     console.log(res);
     if (res.resultStatus == 200) {
       var Data = res.data;
         this.setData({
           amount: (Data.hongbao / 100).toFixed(2),
         })
     } else {
       wx.showToast({
         title: !!res.message.global ? res.message.global : "请求出错",
         icon: "loading"
       })
     }
   })
 },
 //列表
 getList:function(){
   var param={
     "pageNo": this.data.pageNo,
     "pageSize": 6,
   }
   var token = (wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token) || '';
   ajaxhelper.post(app.globalData.frontJSHost + 'hongbao/userHistory?token='+ encodeURIComponent(token), param, this, function (res) {
     console.log(res);
     if (res.resultStatus == 200) {
       var result = res.data.result;
       var pageNo = this.data.pageNo;
       var hasmore = res.data.totalCount - pageNo * res.data.pageSize;
       this.setData({
         pageNo: ++pageNo,
         hasRefresh: false,
         isGetMore: false,
         hasMore: hasmore
       })
       for (var i = 0; i < result.length; i++) {
         var createDate = result[i].hongbaoGetRecord.createDate;
         var time = new Date(+createDate).format("yyyy-MM-dd hh:mm:ss");
         result[i].hongbaoGetRecord.time = time;
       }
       if (res.data && result) {
         this.setData({
           dataList: this.data.dataList.concat(result)
         })
       }
     } else {
       wx.showToast({
         title: !!res.message.global ? res.message.global : "请求出错",
         icon: "loading"
       })
     }
   })
 },

 //下拉刷新
 onPullDownRefresh() {
   this.setData({
     isShow: false
   })
   if (this.data.hasRefresh) return false;
   this.setData({
     dataList: [],
     hasRefresh: true,
     pageNo: 1
   })
   this.getList()
   setTimeout(function () {
     wx.stopPullDownRefresh()
   }, 1400);

 },
 //上拉加载
 onReachBottom() {
   if (this.data.isGetMore) return false;
   if (this.data.hasRefresh) return false;
   if (this.data.hasMore > 0) {
     this.setData({
       isGetMore: true,
       // tip: '正在加载中...'
     })
     this.getList();
   } else {
     this.setData({
       isGetMore: false,
       // tip: '没有更多数据了...'
     })
     return false
   }
 }

})
